473,419 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Single Apostrophe in a VBA code; syntax error

I have a field in a database that contains last names. In some of those names, like O'Brien and O'Connor, there is a ' symbol.

I am using combo boxes on a form to build a form filter and in particular, I receive a Runtime 3075 Syntax Error : Missing Operator in query expression '([Player_Name] Like '*Jim O'Toole*')' and I know it stems from the apostrophe in the name box, but I don't know how to fix it.

The combo box that contains the list of names is cboPlayer. Here is my VBA code as current with non-related details omitted. I have a cboActions box that contains what output I wish to have based on the combo-entries. I have a command button Perform that executes the following code upon Click. How do I modify the code to account for any data-necessary apostrophes?


Expand|Select|Wrap|Line Numbers
  1. Private Sub Perform_Click()
  2.     Dim SCstrFilter As String
  3.  
  4.     'cboPlayer - Hard Text
  5.     If Me!cboPlayer > "" Then _
  6.         SCstrFilter = SCstrFilter & " AND ([Player_Name] Like '*" & Me!cboPlayer & "*')"
  7.  
  8.     'Tidy up results and apply IF NECESSARY
  9.     If SCstrFilter > "" Then SCstrFilter = Mid(SCstrFilter, 6)
  10.  
  11.     If cboActions = "Add Stock Items" Then
  12.         DoCmd.OpenForm "Catalog", , , SCstrFilter   (This is where the VBA debug window points to with the error)
  13.     End If
Dec 23 '09 #1
7 18647
ChipR
1,287 Expert 1GB
You can use quotes to delimit a string with an apostrophe inside. To put a quote in a string, use two quotes in a row, and the string will contain one. This works:
Expand|Select|Wrap|Line Numbers
  1. ..." AND Player_Name Like ""*" & Me.cboPlayer & "*"")"
Dec 23 '09 #2
Thank you; that works perfectly.
Dec 23 '09 #3
NeoPa
32,556 Expert Mod 16PB
Check out Quotes (') and Double-Quotes (") - Where and When to use them.

Doubling up each is always an option when dealing with a quote character embedded in a value. Indeed this is recommended above the sometimes suggested alternative of treating the different types of quotes as interchangeable (which only ever seems to be the case within a Microsoft environment). There is nothing anti-MS about this. It is simply about fundamental logic and portability of code (Always a good idea, even when not planning on taking advantage of it).
Dec 24 '09 #4
OldBirdman
675 512MB
Because a name can have either type of quote, a more general solution should be considered. If names like Pete O'Toole or Tom "Bud" Adams can occur, all methods shown will fail.

How about -
Expand|Select|Wrap|Line Numbers
  1. "Player_Name Like '*" & Replace(Me.cboPlayer, "'", "''") & "*'"
Note: Replace(#1, #2, #3) arguments are:
#1: Control or Variable
#2: One single quote between double quotes
#3: Two single quotes between double quotes
Dec 24 '09 #5
NeoPa
32,556 Expert Mod 16PB
all methods shown will fail.
I'm confused OB.

Two main reasons :
  1. Your solution only doubles up the single-quotes. This isn't a problem, as discussed below. However you indicated earlier (quoted text) that this wouldn't work.
  2. Your solution is a perfect example of the solution I had proposed in my previous post.
What is strange is that actually this solution does work in all circumstances. There is no need to handle double-quotes specially, as you used the correct (standard) quotes around the string in the first place. Thus the double-quotes are treated without ambiguity by the parsing engine.

As a clarification for anyone still unconvinced about using the standard quotes (after all Access adds the non-standard ones in for you in many places) if this were to be done using these (double-quotes) instead, then OB's code would look like :
Expand|Select|Wrap|Line Numbers
  1. "Player_Name Like ""*" & Replace(Me.cboPlayer, """", """""") & "*"""
I hardly need say that I don't recommend this approach, but it may be helpful for those still struggling with the concept but unwilling to take on the standard approach.

NB. All quotes in this example are Double-Quotes.
Jan 2 '10 #6
OldBirdman
675 512MB
When I substitute both my names, Pete O'Toole or Tom "Bud" Adams, into ChipR's formula, I get:
Expand|Select|Wrap|Line Numbers
  1. (Player_Name Like "*Pete O'Toole*")
which is valid, but:
Expand|Select|Wrap|Line Numbers
  1. (Player_Name Like "*Tom "Bud" Adams*")
which is not a valid string.

1. Your solution only doubles up the single-quotes. .
.
True. But the string is delimited with single quotes, so there is no reason to double the double-quote. Actually, it would be an error to do so.
2. Your solution is a perfect example of the solution I had proposed in my previous post.
I don't see a solution in your post. Looking at your link, I cannot find where it addresses inserting a string into another string where either the single- or double-quote may occur in the inserted string. It does address either the single-quote within single quotes, and double-quotes within double quotes. I was proposing code that would do what this article infers.
I entered this thread because I felt the solution given left a condition unresolved. That condition was a name with a double-quote within it. My telephone book, latest election ballot, and my personal address book have names that have single-quotes and names with double-quotes. I could not see why HSXWillH might not have such names in the future, although apparently not yet.
I believe that testing should include as many conditions as the developer can think of, and not just the conditions that exist currently. I did not think that HSXWillH had thought of the name list containing the double-quote. Therefore, the statement

Thank you; that works perfectly.
after ChipR's post really should be
Thank you; that works perfectly for the example given.
Jan 2 '10 #7
NeoPa
32,556 Expert Mod 16PB
I suppose that makes sense if you didn't see a recommended solution in my post. Let me be more explicit :

If you look at the linked article you will find the point expressed clearly in the section titled Embedded Quotes (General), albeit the example given was not specifically for SQL, the text explains the concept that works generally regardless of which quote character is used.

In the post itself, I say
Doubling up each is always an option when dealing with a quote character embedded in a value.
No example is given clearly, but the concept is expressed immediately after the link to the article where there is a usable example.

Lastly, it seems you think I was critical of the solution you proposed. That's entirely not what I was trying to say. I was saying indeed that it's perfect. I simply commented that it was a reiteration of what I'd already posted. Not a problem. A useful example was provided, but I was simply confused at your comment that previous solutions were wrong, which I find difficult to comprehend in the circumstances.
Jan 3 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
1
by: Ronny Sigo | last post by:
Hello all, I have made a form containing a combox which must look up values (names) in a table. It works fine until the moment I try to put in a name containg an apostrophe. (e.g. d'Haen). At this...
5
by: Berend | last post by:
when I try to write to a database I get a syntax error I made the code a simple as possible bur also the VS generated code gives the same error. WHY? private void button1_Click(object sender,...
4
Tog
by: Tog | last post by:
Hello, I have read several posts with this heading but none of them have helped. I have the following error message: Syntax error (missing operator) in query expression 'left(Your Product...
9
by: camphor | last post by:
hi, I am very new to PHP and trying to set up a virtual host in Apache so I can test the protx Form PHP Intergration kit I received this message after I restarted Apache: Syntax error on...
5
by: gershwyn | last post by:
I'm trying to make a very simple order form. I have two tables - OrderHeader has information specific to the order itself, such as company name and order date. OrderDetail contains details such as...
1
by: inbarik | last post by:
Hi, i'm getting syntax error message for this button that tries to get name (of building) with apostrophe. Any idea how to solve this? Note that i must use name with apostrophe e.g. O'neal: ...
1
by: P Kerr | last post by:
I am new to PHP and have written the following php code for an entry form on my website: if ($_FILES == 0) { // Move the file to the target upload folder $target = WS_UPLOADPATH . $photo;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.